# 注意事项

  1. 拷贝文件既可能是文本文件又可能是二进制文件,为了通用性,使用字节流
  2. 拷贝文件可能极大,不能一次读取,因此采用边读边写
  3. 目标文件可能不存在,需要进行异常处理
  4. 拷贝后的文件目录可能不存在,需要创建目录

# 样例代码

class CopyUtil {
    private File srcFile;
    private File destFile;

    public CopyUtil(String[] args) {
        if (args.length != 2) {
            throw new IllegalArgumentException("Need 2 arguments!");
        }
        this.srcFile = new File(args[0]);
        this.destFile = new File(args[1]);
    }

    /**
     * 实现拷贝操作的过程
     * @return 本次拷贝所用时间
     * @throws FileNotFoundException 源文件不存在
     */
    public long copy() throws IOException {
        long start = System.currentTimeMillis();
        if (!this.srcFile.exists()) {
            throw new FileNotFoundException("Source file not exist!");
        }
        if (!this.destFile.getParentFile().exists()) {
            this.destFile.getParentFile().mkdirs();
        }
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(this.srcFile);
            output = new FileOutputStream(this.destFile);
            byte[] data = new byte[1024];
            int len = 0;  // 读取的字节个数
            while ((len = input.read(data)) != -1) {
                output.write(data, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long end = System.currentTimeMillis();
        return end - start;
    }
}

public class Driver {
    public static void main(String[] args) {
        CopyUtil cu = new CopyUtil(args);
        try {
            System.out.println(cu.copy());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

JDK1.9 优化:使用 transferTo 新方法,系统自主选择合适大小的 buffer 进行分段拷贝

try {
    input = new FileInputStream(this.srcFile);
    output = new FileOutputStream(this.destFile);
    input.transferTo(output);
}
1
2
3
4
5
Last Updated: 7/2/2020, 4:20:51 PM